<template>
	<div>
    <div
      class="search_template"
      style="background:white;height:120px;"
    >
      <div
        class="content"
        style="height: 80px; display: flex; justify-content: space-around;align-items: center;">
        <div>
          <span style="color:#888; font-weight:800;">角色名称:</span>
          <el-input
            v-model="search"
            placeholder="请输入角色名称搜索"
            @keyup.enter.native="onEnterSearch"
            style="width:200px;height50px;"
          />
        </div>
        <div align="right">
          <el-button size="mini" icon="el-icon-search" type="success" @click="onEnterSearch">点击查询</el-button>
        </div>
      </div>
      <div align="left" style="margin-left:10px">
        <el-button size="mini" type="primary" plain icon="el-icon-plus" @click="dialogFormVisible_two = true">新增</el-button>
      </div>
    </div>
		<el-container style="height: 500px; border: 1px solid #eee">
			<el-container>
				<!-- <router-view></router-view> -->
                <!-- .filter(data => !search || data.name.toLowerCase().includes(search.toLowerCase())) -->
				<el-table
					:data="tableData"
                    border 
					style="width: 100%;"
				>
					<el-table-column align="center" label="角色编码" prop="roleCode"></el-table-column>
					<el-table-column align="center" label="角色名称" prop="roleName"></el-table-column>
					<el-table-column align="center" label="角色描述" prop="roleDescribe"></el-table-column>
					<el-table-column align="center" label="操作">
						<template slot-scope="scope">
							<el-button size="mini" type="primary" plain @click="modify_display(scope.row.roleId)">修改</el-button>
							<el-button size="mini" type="danger" plain @click="open(scope.row.roleId)">删除</el-button>
						</template>
					</el-table-column>
				</el-table>
				<el-dialog title="新增功能" :visible.sync="dialogFormVisible_two" :before-close="close">
					<!-- 	<el-dialog title="新增功能" :destroy-on-close="true" :visible.sync="dialogFormVisible_two"> -->
					<el-form
						:model="dynamicValidateForm"
						ref="dynamicValidateForm"
						label-width="100px"
						class="demo-dynamic"
					>
						<el-form-item
							prop="new_role_name"
							label="角色名称"
							:rules="[
			      { required: true, message: '请输入角色名称', trigger: 'blur' },
			    
			    ]"
						>
							<el-input v-model="dynamicValidateForm.new_role_name"></el-input>
						</el-form-item>
						<el-form-item
							prop="name_code"
							label="角色编码"
							:rules="[
			      { required: true, message: '请输入角色编码', trigger: 'blur' },
			    
			    ]"
						>
							<el-input v-model="dynamicValidateForm.name_code"></el-input>
						</el-form-item>
						<el-form-item
							label="角色描述"
							prop="desc"
							:rules="[
				  { required: true, message: '请输入角色描述', trigger: 'blur' },
				
				]"
						>
							<el-input type="textarea" v-model="dynamicValidateForm.desc"></el-input>
						</el-form-item>
						<el-form-item
							label="角色权限"
							:rules="[
				  { required: true, message: '请选择角色权限', trigger: 'blur' },
				
				]"
							style="height:150px;overflow-y:scroll;"
						>
							<!--  <el-select v-model="dynamicValidateForm" placeholder="请选择">
				    <el-option label="权限1" value="shanghai"></el-option>
				    <el-option label="权限2" value="beijing"></el-option>
							</el-select>-->
							<!--树形结构-->
							<el-tree
								:data="data"
								show-checkbox
								node-key="id"
								ref="tree"
								:accordion="true"
								:expand-on-click-node="false"
								:render-content="renderContent"
							></el-tree>
							<!--  <div class="buttons">
					    <el-button @click="getCheckedKeys">通过 key 获取</el-button>
							</div>-->
							<!--树形结构-->
						</el-form-item>
					</el-form>
					<div slot="footer" class="dialog-footer">
						<el-button @click="close">取消</el-button>
						<el-button type="primary" @click="new_onSubmit(dynamicValidateForm)">确定</el-button>
						<!-- <el-button @click="dialogFormVisible_two = false">取 消</el-button>
						<el-button type="primary" @click="dialogFormVisible_two = false">确 定</el-button>-->
					</div>
				</el-dialog>
				<!--修改功能-->
				<el-dialog title="修改功能" :visible.sync="dialogFormVisible_three" :destroy-on-close="true">
					<el-form
						:model="dynamicValidateForm_modify"
						ref="dynamicValidateForm_modify"
						label-width="100px"
						class="demo-dynamic"
					>
						<el-form-item
							prop="new_role_name_modify"
							label="角色名称"
							:rules="[
			      { required: true, message: '请输入角色名称', trigger: 'blur' },
			    
			    ]"
						>
							<el-input v-model="dynamicValidateForm_modify.new_role_name_modify"></el-input>
						</el-form-item>
						<el-form-item
							prop="name_code_modify"
							label="角色编码"
							:rules="[
			     { required: true, message: '请输入角色编码', trigger: 'blur' },
			   
			   ]"
						>
							<el-input v-model="dynamicValidateForm_modify.name_code_modify"></el-input>
						</el-form-item>
						<el-form-item
							label="角色描述"
							prop="desc_modify"
							:rules="[
				  { required: true, message: '请输入角色描述', trigger: 'blur' },
				
				]"
						>
							<el-input type="textarea" v-model="dynamicValidateForm_modify.desc_modify"></el-input>
						</el-form-item>
						<el-form-item
							label="角色权限"
							:rules="[
				  { required: true, message: '请选择角色权限', trigger: 'blur' },
				
				]"
							style="height:150px;overflow-y:scroll;"
						>
							<!--树形结构-->
							<el-tree
								:data="data"
								show-checkbox
								node-key="id"
								:accordion="true"
								ref="tree_modify"
								:expand-on-click-node="false"
								:render-content="renderContent"
							></el-tree>
							<!--树形结构-->
						</el-form-item>
					</el-form>
					<div slot="footer" class="dialog-footer">
						<el-button type="primary" @click="onSubmit_modify(dynamicValidateForm_modify)">确定</el-button>
						<el-button @click="dialogFormVisible_three = false">取消</el-button>
					</div>
				</el-dialog>
				<!--修改功能-->
			</el-container>
		</el-container>
		<el-container style="margin-top:20px;margin-bottom:20px;float:right;margin-right:20px">
			<el-pagination
				@size-change="handleSizeChange"
				@current-change="handleCurrentChange"
				:current-page="currentPage"
				:page-sizes="[5, 10, 20, 40]"
				:page-size="pagesize"
				layout="total,sizes, prev, pager, next, jumper"
				:total="total_one"
			></el-pagination>
		</el-container>
	</div>
</template>
<style>
.el-header {
    background-color: #b3c0d1;
    color: #333;
    line-height: 60px;
}

.el-aside {
    color: #333;
}
</style>

<script>
import {
    getUserInfo,
    deleteUser,
    getUserJurisdiction,
    add_role_role,
    get_role_jurisdiction,
    getModifyUserInfo,
    modify_role_role,
    getRoleSearch
} from '@/api/user';
export default {
    data() {
        return {
            tableData: [],
            search: '',
            dialogTableVisible: false,
            dialogFormVisible: false,
            dialogTableVisible_two: false,
            dialogFormVisible_two: false,
            dialogTableVisible_three: false,
            dialogFormVisible_three: false,
            /*****添加功能的form******/
            dynamicValidateForm: {
                new_role_name: '',
                name_code: '',
                desc: ''
            },
            /******添加功能的form****/
            /******修改的form*******/
            dynamicValidateForm_modify: {
                new_role_name_modify: '',
                name_code_modify: '',
                desc_modify: ''
            },
            role_id_id: '',
            /******修改的form*******/

            data: [],
            /********分页数据********/
            currentPage: 1,
            pagesize: 10,
            total_one: 0
            // isShow:true
            /********分页数据*******/
        };
    },

    /********获取用户列表***********/
    created() {
        // var getUserInfo_params={
        // 	token: "eyJhbGciOiJIUzUxMiJ9.eyJsb2dpbl91c2VyX2tleSI6IjM5MzczZDUwLWRkZWUtNDMzOC1iNWE1LWI5OGE0NzBjNzY3NSJ9.s-rok9MHQpnLH8unFriyHwc8_8NBFeCR7zzj-sgbdUR9u-QZxu-zINqGOj2Okv74UftPbro2pAqTYrGtwPfbjw"
        // }
        this.load_jiaose_list();

        /**********获取添加角色里的权限************/
        get_role_jurisdiction()
            .then(response => {
                if (response.data.code == 200) {
                    // this.tableData=response.data.rows
                    this.data = response.data.menus;
                } else {
                    this.$message.error(response.data.msg);
                }
            })
            .catch(() => {});
        /**********获取添加角色里的权限***********/
    },
    /********获取用户列表**********/
    /**********删除功能***************/
    methods: {
        close() {
            this.$refs.dynamicValidateForm.resetFields();
            this.dialogFormVisible_two = false;
            this.$refs.tree.setCheckedKeys([]);
        },
        /******导航栏跳转功能********/
        handleSelect() {},
        handleOpen(key, keyPath) {},
        handleClose(key, keyPath) {},
        /******导航栏跳转功能*********/
        open(index) {
            this.$confirm('您确定要删除这条信息吗?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
                center: true
            })
                .then(() => {
                    /*****点击了确认*******/

                    var roleId = {
                        roleIds: [index]
                    };
                    deleteUser(roleId)
                        .then(response => {
                            if (response.data.code == 200) {
                                // var getUserInfo_params={
                                // 	token: "eyJhbGciOiJIUzUxMiJ9.eyJsb2dpbl91c2VyX2tleSI6IjM5MzczZDUwLWRkZWUtNDMzOC1iNWE1LWI5OGE0NzBjNzY3NSJ9.s-rok9MHQpnLH8unFriyHwc8_8NBFeCR7zzj-sgbdUR9u-QZxu-zINqGOj2Okv74UftPbro2pAqTYrGtwPfbjw"
                                // }
                                this.load_jiaose_list();
                                this.$message({
                                    message: '删除成功',
                                    type: 'success'
                                });
                            } else {
                                this.$message.error(response.data.msg);
                            }
                        })
                        .catch(() => {});
                    /******点击了确认********/
                })
                .catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
        },
        /********删除功能****************/

        onSubmit() {},

        /**********修改功能数据回显*************/
        /*******树形结构********/
        renderContent(h, { node, data, store }) {
            return (
                <span class="custom-tree-node">
                    <span>{node.label}</span>
                </span>
            );
        },

        /********树形结构**********/
        /**********添加角色的功能****************/
        new_onSubmit(dynamicValidateForm) {
            this.dialogFormVisible_two = false;
            this.$refs.dynamicValidateForm.validate(valid => {
                // console.log("我是现在需要的数据",valid);
                var new_add_params = {
                    roleName: dynamicValidateForm.new_role_name,
                    roleCode: dynamicValidateForm.name_code,
                    roleDescribe: dynamicValidateForm.desc,
                    menuIds: this.$refs.tree.getCheckedKeys()
                };
                this.$refs['dynamicValidateForm'].resetFields();
                if (valid) {
                    add_role_role(new_add_params)
                        .then(response => {
                            if (response.data.code == 200) {
                                /******清空form表单********/

                                /******清空form表单*******/
                                this.load_jiaose_list();
                                this.$message({
                                    message: '添加成功',
                                    type: 'success'
                                });
                            } else {
                                this.$message.error(response.data.msg);
                            }
                        })
                        .catch(() => {});
                } else {
                    this.$message.error('缺少必要的参数');
                }

                /*******调用新增接口*********/
            });
        },
        /*********添加角色的功能**************/
        /********加载角色列表********/
        load_jiaose_list() {
            var params_list = {
                pageNum: this.currentPage,
                pageSize: this.pagesize
            };
            getUserInfo(params_list)
                .then(response => {
                    if (response.data.code == 200) {
                        this.tableData = response.data.data.list;
                        this.total_one = response.data.data.total;
                    } else {
                        this.$message.error(response.data.msg);
                    }
                })
                .catch(() => {});
        },
        /*******加载角色列表********/
        /**********角色修改功能*************/
        /********修改数据回显********/
        modify_display(role_id) {
            this.role_id_id = role_id;
            this.dialogFormVisible_three = true;
            var getUserInfo_params = {
                roleId: role_id
            };

            getModifyUserInfo(getUserInfo_params)
                .then(response => {
                    if (response.data.code == 200) {
                        console.log('我是修改的回显', response);

                        this.dynamicValidateForm_modify.new_role_name_modify = response.data.data.roleName;

                        this.dynamicValidateForm_modify.name_code_modify = response.data.data.roleCode;
                        this.dynamicValidateForm_modify.desc_modify = response.data.data.roleDescribe;
                    } else {
                        this.$message.error(response.data.msg);
                    }
                })
                .catch(() => {});
        },
        /********修改数据的回显********/
        onSubmit_modify(dynamicValidateForm_modify) {
            this.dialogFormVisible_three = false;
            // this.$refs[dynamicValidateForm].validate((valid) => {

            var new_add_params = {
                roleId: this.role_id_id,
                roleName: dynamicValidateForm_modify.new_role_name_modify,
                roleCode: dynamicValidateForm_modify.name_code_modify,
                roleDescribe: dynamicValidateForm_modify.desc_modify,
                menuIds: this.$refs.tree_modify.getCheckedKeys()
            };

            this.$refs.dynamicValidateForm_modify.validate(valid => {
                this.$refs['dynamicValidateForm_modify'].resetFields();
                if (valid) {
                    modify_role_role(new_add_params)
                        .then(response => {
                            if (response.data.code == 200) {
                                this.load_jiaose_list();
                                this.$message({
                                    message: '修改成功',
                                    type: 'success'
                                });
                            } else {
                                this.$message.error(response.data.msg);
                            }
                        })
                        .catch(() => {});
                } else {
                    this.$message.error('缺少必要的参数');
                }
            });

            /*******调用新增接口*********/
        },
        /*********角色修改功能************/
         onEnterSearch(e, searchVal) {
            /***********调用模糊查询接口***************/
            var new_add_params = {
                pageNum: 1,
                pageSize: 10,
                roleName: this.search, //搜索
               
            };
            getRoleSearch(new_add_params)
                .then(response => {
                    if (response.data.code == 200) {
                        this.tableData = response.data.data.list;
                        this.$message({
                            message: '查询成功',
                            type: 'success'
                        });
                    } else {
                        this.$message.error(response.data.msg);
                    }
                })
                .catch(() => {});
            /*********调用模糊查询的接口**************/
        },
        /**********模糊查询搜索功能****************/
        /********我是分页************/
        handleSizeChange: function(size) {
            this.pagesize = size;
            this.load_jiaose_list();
        },
        handleCurrentChange: function(currentPage) {
            this.currentPage = currentPage;
            this.load_jiaose_list();
        }
        /*********我是分页*********/
    }
};
</script>